package barbershop.com.service.impl;

import barbershop.com.dao.CashierDao;
import barbershop.com.entity.Cashier;
import barbershop.com.service.CashierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CashierServiceImpl implements CashierService {
    @Autowired
    private CashierDao cashierDao;

    @Override
    public Cashier insert(Cashier record) {
        Cashier cashier = new Cashier();
        try {
            cashier = this.cashierDao.save(record);
            System.out.println("插入成功！");
            return cashier;
        }   catch (Exception e){
            System.out.println("插入失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Boolean delete(Cashier record) {
        try {
            this.cashierDao.delete(record);
            System.out.println("删除成功！");
            return true;
        }   catch (Exception e){
            System.out.println("删除失败");
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Cashier update(Cashier record) {
        Cashier cashier = new Cashier();
        try {
            cashier = this.cashierDao.save(record);
            System.out.println("修改成功！");
            return cashier;
        }   catch (Exception e){
            System.out.println("修改失败！");
            e.printStackTrace();
        }
        return cashier;
    }

    @Override
    public Cashier find(Cashier record) {
        Optional<Cashier> cashier;
        try {
            cashier = this.cashierDao.findById(record.getCasId());
            if (cashier.isPresent()) {
                System.out.println("查找成功！");
                return cashier.get();
            }
            return null;
        }   catch (Exception e){
            System.out.println("查找失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Cashier findById(Integer casId) {
        Optional<Cashier> cashier;
        try {
            cashier = this.cashierDao.findById(casId);
            if (cashier!=null){
                System.out.println("用户查找成功！");
                return cashier.get();
            }
            return null;
        }   catch (Exception e){
            System.out.println("用户查找失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Cashier login(String casName, String casPwd) {
        Cashier cashier = new Cashier();
        try {
            cashier = this.cashierDao.casLogin(casName, casPwd);
            if (cashier!=null){
                System.out.println("收银员"+cashier.getCasName()+"验证成功！");
                return cashier;
            }
            return null;
        }   catch (Exception e){
            System.out.println("收银员不存在！");
            e.printStackTrace();
        }
        return null;
    }
}
